package com.bishe.controller;

import cn.hutool.core.util.StrUtil;
import com.bishe.pojo.Apply;
import com.bishe.pojo.Checkwork;
import com.bishe.pojo.QueryModel;
import com.bishe.pojo.User;
import com.bishe.serivce.ICheckworkService;
import com.bishe.serivce.IUserService;
import com.bishe.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collector;

/**
 * 考情控制层
 * @date 2022/03/20 42:08:04
 */
@RestController
@RequestMapping("/checkwork")
@Api(tags = "考勤控制器")
@CrossOrigin
public class CheckworkController {
    @Autowired
    private ICheckworkService checkworkService;


    @Autowired
    private IUserService userService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @ApiOperation("添加和修改考勤")
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Checkwork checkwork, HttpServletRequest httpServletRequest){
        //登录验证
        JobUtil.checkUserLogin(httpServletRequest,jdbcTemplate);
        if (checkwork == null){
            throw new EcoBootException("checkwork 为空！");
        }
        checkworkService.addOrUpdate(checkwork,httpServletRequest);

        return Result.ok();
    }

    @ApiOperation("删除考勤")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id")String id,HttpServletRequest request){
        //管理员验证登录
        JobUtil.checkAdminLogin(request,jdbcTemplate);
        checkworkService.deleteById(id);

        return Result.ok();
    }

    @ApiOperation("考勤分页 内容模糊")
    @GetMapping("/getPage")
    public Result getPage(QueryModel queryModel,HttpServletRequest request){
        //检查登录
        String userType = JobUtil.getUserType(request);
        if (StrUtil.isEmpty(userType)||!"经理".equals(userType)){
            throw new NotLoginException("未登录！");
        }

        List<Checkwork> data = null;
        Integer pageNo = queryModel.getPageNo();
        Integer pageSize = queryModel.getPageSize();
        String searchTxt = queryModel.getSearchTxt();
        String createBy = queryModel.getCreateBy();
        SqlQueryUtils<Checkwork> applySqlQueryUtils = new SqlQueryUtils<Checkwork>(Checkwork.class, jdbcTemplate);

        if (pageNo == null){
            pageNo = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }
        if (StrUtil.isNotEmpty(searchTxt)){
            applySqlQueryUtils.like(Checkwork::getContent,searchTxt);
        }

        if (StrUtil.isNotEmpty(createBy)){
            List<User> users = userService.likeName(createBy);
            applySqlQueryUtils.in(Checkwork::getCreateBy,users);
        }
        applySqlQueryUtils.orderByDesc(Checkwork::getCreateTime);
        //都是0 不分页
        if (pageNo == 0 && pageSize == 0){
            data  =  applySqlQueryUtils.getList();
        }else {
            data =  applySqlQueryUtils.limit(pageNo,pageSize).getList();
        }
        for (Checkwork s : data) {
            String createBy1 = s.getCreateBy();
            User one = new SqlQueryUtils<User>(User.class, jdbcTemplate).eq(User::getId, createBy1).getOne();
            if(one!=null){
                s.setCreateByName(one.getNikName());
            }
        }

        return Result.ok(new PageResult<Checkwork>(queryModel,applySqlQueryUtils,data));
    }


    @ApiOperation("获取自己的考勤")
    @PostMapping("/getCheckwork")
    public Result getCheckwork(QueryModel queryModel,HttpServletRequest request){
        //检查登录
        String userType = JobUtil.getUserType(request);
        if (StrUtil.isEmpty(userType)||!"员工".equals(userType)){
            throw new NotLoginException("未登录！");
        }
        List<Checkwork> data = null;
        Integer pageNo = queryModel.getPageNo();
        Integer pageSize = queryModel.getPageSize();
        String searchTxt = queryModel.getSearchTxt();
        SqlQueryUtils<Checkwork> applySqlQueryUtils = new SqlQueryUtils<Checkwork>(Checkwork.class, jdbcTemplate);

        if (pageNo == null){
            pageNo = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }
        if (StrUtil.isNotEmpty(searchTxt)){
            applySqlQueryUtils.like(Checkwork::getContent,searchTxt);
        }
        applySqlQueryUtils.eq(Checkwork::getCreateBy, JobUtil.getUserId(request));
        applySqlQueryUtils.orderByDesc(Checkwork::getCreateTime);
        //都是0 不分页
        if (pageNo == 0 && pageSize == 0){
            data  =  applySqlQueryUtils.getList();
        }else {
            data =  applySqlQueryUtils.limit(pageNo,pageSize).getList();
        }
        for (Checkwork s : data) {
            String createBy1 = s.getCreateBy();
            User one = new SqlQueryUtils<User>(User.class, jdbcTemplate).eq(User::getId, createBy1).getOne();
            if(one!=null){
                s.setCreateByName(one.getNikName());
            }
        }
        return Result.ok(new PageResult<Checkwork>(queryModel,applySqlQueryUtils,data));
    }

}
